package com.wl.mapper;

import com.wl.pojo.Personal.Collect;
import com.wl.pojo.Post;
import com.wl.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface PostMapper {

    /**
     * 发布文章接口
     * 将文章数据写入数据库
     */
    // 标签项
    @Insert("insert into post_tag (post_id, tag_id) values(#{postId}, #{tagId})")
    void addPostTag(Integer postId, Integer tagId);
    // 插入后并返回文章ID
    @Insert("insert into posts(title, cover_img, summary, content, create_time, update_time, user_id, category_id)" +
            "values(#{title}, #{coverImg}, #{summary}, #{content}, #{createTime}, #{updateTime}, #{userId}, #{categoryId})")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="postId", before=false, resultType=Integer.class)
    void publishPost(Post post);

    /**
     * 修改文章接口
     * @param post 修改的信息
     */
    // 修改文章
    @Update("update posts set title = #{title}, cover_img = #{coverImg}, summary = #{summary}, " +
            "content = #{content}, update_time = #{updateTime} where post_id = #{postId}")
    void updatePost(Post post);
    // 修改文章标签
    @Delete("delete from post_tag where post_id = #{postId}")
    void deletePostTag(Integer postId);

    /**
     * 获取标签数据
     */
    @Select("select * from tags")
    List<Map<Integer, String>> getTags();

    /**
     * 获取首页展示的文章数据
     */
    List<Post> getPosts(Integer userId, Integer categoryId, Integer isDescending);
    // 获取每篇文章的标签数据
    @Select("select tag_name as tag_names from post_tag pt join tags t on pt.tag_id = t.tag_id where pt.post_id = #{postId}")
    List<String> getPostTagNames(Integer postId);

    /**
     * 根据浏览量和点赞量进行降序排序查询热点数据
     */
    @Select("select sub_query.post_id, sub_query.title, sub_query.page_view, sub_query.like_count\n" +
            "from (\n" +
            "         select p.*, count(l.user_id) as like_count\n" +
            "         from posts p\n" +
            "                  left join likes l on p.post_id = l.post_id\n" +
            "         group by p.post_id, p.title, p.page_view\n" +
            "     ) sub_query\n" +
            "order by sub_query.page_view desc, sub_query.like_count desc\n" +
            "limit 20;")
    List<Post> getHotPosts();

    /**
     * 根据ID查询文章的详细信息
     */
    @Select("select p.*, count(l.user_id) as like_count, u.username\n" +
            "from posts p\n" +
            "left join likes l on p.post_id = l.post_id\n" +
            "join users u on p.user_id = u.user_id\n" +
            "where p.post_id = #{postId};")
    Post getPostById(Integer postId);

    /**
     * 每触发一次就代表这篇文章的访问量加 1
     */
    @Update("update posts set page_view = page_view + 1 where post_id = #{postId};")
    void addPostViews(Integer postId);

    /**
     * 根据文章id查询文章的作者信息
     */
    @Select("select u.*, count(uf.user_id) as followers_count\n" +
            "from users u\n" +
            "left join user_follow uf on u.user_id = uf.follow_id\n" +
            "where u.user_id = (select user_id from posts where post_id = #{postId})\n" +
            "group by u.user_id;")
    User getUserByPostId(Integer postId);
    // 查询文章数量
    @Select("select count(user_id) from posts where user_id = #{userId}")
    Integer getPostCount(Integer userId);
    // 查询用户标签
    @Select("select it.tags_name from users u\n" +
            "join user_tag on u.user_id = user_tag.user_id\n" +
            "join interest_tags it on user_tag.interest_id = it.interest_id\n" +
            "where u.user_id = #{userId};")
    List<String> getUserTags(Integer userId);

    /**
     * 查看某个用户是否关注了文章作责
     */
    @Select("select count(user_id) from user_follow where user_id = #{userId} and follow_id = #{followId};")
    Integer getIsFollow(Integer userId, Integer followId);

    /**
     * 关注某个用户
     */
    @Insert("insert into user_follow (user_id, follow_id) values (#{userId}, #{followId});")
    void activeFollow(Integer userId, Integer followId);

    /**
     * 取消关注某个用户
     */
    @Delete("delete from user_follow where user_id = #{userId} and follow_id = #{followId};")
    void cancelFollow(Integer userId, Integer followId);

    /**
     * 判断某个用户是否对某篇文章点赞
     */
    @Select("select count(user_id) from likes where user_id = #{userId} and post_id = #{postId};")
    Integer getIsLike(Integer userId, Integer postId);

    /**
     * 点赞请求
     */
    @Insert("insert into likes (user_id, post_id) values (#{userId}, #{postId})")
    void activeLike(Integer userId, Integer postId);

    /**
     * 取消点赞请求
     */
    @Delete("delete from likes where user_id = #{userId} and post_id = #{postId};")
    void cancelLike(Integer userId, Integer postId);
    /**
     * 获取文章有多少点赞数量
     */
    @Select("select count(post_id) from likes where post_id = #{postId}")
    Integer getLikeCount(Integer postId);

    /**
     * 收藏请求
     */
    @Insert("insert into collect (user_id, post_id, category_id) values (#{userId}, #{postId}, #{categoryId})")
    void activeCollect(Integer userId, Integer postId, Integer categoryId);

    /**
     * 取消收藏接口
     */
    @Delete("delete from collect where user_id = #{userId} and post_id = #{postId} and category_id = #{categoryId};")
    void cancelCollect(Integer userId, Integer postId, Integer categoryId);

    /**
     * 查看收藏数量
     */
    @Select("select count(post_id) from collect where post_id = #{postId}")
    Integer getCollectCount(Integer postId);

    /**
     * 判断某个用户是否对某篇文章收藏
     */
    @Select("select count(user_id) from collect where user_id = #{userId} and post_id = #{postId};")
    Integer getIsCollect(Integer userId, Integer postId);

    /**
     * 创建收藏夹
     * @param collect 创建需要的数据
     */
    @Select("insert into collect_category (user_id, category_name, update_time, is_private) " +
            "values (#{userId}, #{categoryName}, #{updateTime}, #{isPrivate});")
    void createCategory(Collect collect);
}
